﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_Phim : System.Web.UI.Page
{
    ClassPhim cp = new ClassPhim();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            if (Session["user"] != null)
            {
                GridView5.DataSource = cp.DanhSachCD();
                GridView5.DataBind();
                DropDownList4.DataSource = cp.TheLoaiPhim();
                DropDownList4.DataTextField = "TenTL";
                DropDownList4.DataValueField = "MaTL";
                DropDownList4.DataBind();
            }
            else
                Response.Redirect("DangNhap.aspx");
        }
    }
    protected void BtnTim_Click(object sender, EventArgs e)
    {
        GridView5.DataSource = cp.TimPhim(TextBox1.Text, int.Parse(DropDownList4.SelectedValue), TextBox2.Text, TextBox3.Text);//,int.Parse( TextBox4.Text),int.Parse( TextBox6.Text));,int.Parse( TextBox5.Text),int.Parse(TextBox7.Text));
        GridView5.DataBind();
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        Response.Redirect("ThemCD.aspx");
    }
    protected void GridView5_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView5.EditIndex = -1;
        GridView5.DataSource = cp.DanhSachCD();
        GridView5.DataBind();
    }
    protected void GridView5_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView5.EditIndex = e.NewEditIndex;
        GridView5.DataSource = cp.DanhSachCD();
        GridView5.DataBind();

        DropDownList ddl1 = (DropDownList)GridView5.Rows[e.NewEditIndex].Cells[2].FindControl("DropDownList1");
        ddl1.DataSource = cp.TheLoaiCD();
        ddl1.DataTextField = "TenLoai";
        ddl1.DataValueField = "MaLoai";
        ddl1.DataBind();
        ddl1.Items.FindByValue(GridView5.DataKeys[e.NewEditIndex].Values[0].ToString()).Selected = true;

        DropDownList ddl2 = (DropDownList)GridView5.Rows[e.NewEditIndex].Cells[3].FindControl("DropDownList2");
        ddl2.DataSource = cp.TheLoaiPhim();
        ddl2.DataTextField = "TenTL";
        ddl2.DataValueField = "MaTL";
        ddl2.DataBind();
        ddl2.Items.FindByValue(GridView5.DataKeys[e.NewEditIndex].Values[1].ToString()).Selected = true;

    }
    protected void GridView5_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int macd = int.Parse(GridView5.Rows[e.RowIndex].Cells[1].Text);
        
        TextBox txtTen = (TextBox)GridView5.Rows[e.RowIndex].Cells[2].Controls[0];
        string ten = txtTen.Text;

        DropDownList ddl1 = (DropDownList)GridView5.Rows[e.RowIndex].Cells[3].FindControl("DropDownList1");
        string loai = ddl1.SelectedValue;

        DropDownList ddl2 = (DropDownList)GridView5.Rows[e.RowIndex].Cells[4].FindControl("DropDownList2");
        string tloai = ddl2.SelectedValue;

        TextBox txtdv = (TextBox)GridView5.Rows[e.RowIndex].Cells[5].Controls[0];
        string dv = txtdv.Text;

        TextBox txttap = (TextBox)GridView5.Rows[e.RowIndex].Cells[6].Controls[0];
        string tap=txttap.Text;

        TextBox txtbs = (TextBox)GridView5.Rows[e.RowIndex].Cells[7].Controls[0];
        string bs = txtbs.Text;

        FileUpload fup = (FileUpload)GridView5.Rows[e.RowIndex].Cells[8].FindControl("FileUpload1");
        string hanh = GridView5.DataKeys[e.RowIndex].Values[2].ToString();
        if (fup.HasFile)
        {
            hanh = Path.GetFileName(fup.PostedFile.FileName);
            fup.PostedFile.SaveAs(Server.MapPath("../Pictures/CD/" + hanh));
        }

        TextBox txtgt = (TextBox)GridView5.Rows[e.RowIndex].Cells[9].Controls[0];
        string gt = txtgt.Text;

        TextBox txtnsx = (TextBox)GridView5.Rows[e.RowIndex].Cells[10].Controls[0];
        string nsx = txtnsx.Text;
        
        TextBox txtqg = (TextBox)GridView5.Rows[e.RowIndex].Cells[11].Controls[0];
        string qg = txtqg.Text;

        //if (int.Parse(nsx) < 1900 || int.Parse(nsx) > 2011 || int.Parse(tap) < 1 || int.Parse(bs) < 1)
        //    Label3.Text = "Số nhập không hợp lệ";
        //else
            cp.SuaCD(macd, ten, loai, tloai, dv, int.Parse(tap), int.Parse(bs), hanh, gt,nsx,qg);

        GridView5.EditIndex = -1;
        GridView5.DataSource = cp.DanhSachCD();
        GridView5.DataBind();
    }
    protected void GridView5_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView5.PageIndex = e.NewPageIndex;
        GridView5.DataSource = cp.DanhSachCD();
        GridView5.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < GridView5.Rows.Count; i++)
            {
                CheckBox chb = (CheckBox)GridView5.Rows[i].Cells[0].FindControl("CheckBox1");
                if (chb.Checked == true)
                {
                    int ma = Convert.ToInt32(GridView5.Rows[i].Cells[1].Text);
                    cp.XoaPhim(ma);
                }
            }
        }
        catch (Exception ex)
        {
            Label3.Text = ex.Message;

        }
        GridView5.DataSource = cp.DanhSachCD();
        GridView5.DataBind();
    }
}
